package com.example.Lezedata.ui.activity.login;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.common.TransitionPage;
import com.example.Lezedata.util.GBToast;
import com.example.Lezedata.util.GBUtil;

import bid.Bid;

public class SetPsdVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_psd_vc;
    }

    @Override
    public CharSequence title() {
        return "设置密码";
    }

    private EditText NewPassword;
    private EditText Confirm;
    private Button Submit;
    boolean isPhoneLogin = true;

    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        NewPassword = findViewByTag("NewPassword");
        Confirm = findViewByTag("confirm");
        Submit = findViewByTag("submit");
        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    void submit(){
        String code = getIntent().getStringExtra("code");
        String phone = getIntent().getStringExtra("phone");
        String phoneCode = getIntent().getStringExtra("phoneCode");
        isPhoneLogin = getIntent().getBooleanExtra("isPhoneLogin",isPhoneLogin);

        String psd = NewPassword.getText().toString();
        String confirmPsd = Confirm.getText().toString();

        if (GBUtil.stringIsEmpty(psd)){
            GBToast.shortToast("新密码不能为空");
            return;
        }

        if (GBUtil.stringIsEmpty(confirmPsd)){
            GBToast.shortToast("确认密码不能为空");
            return;
        }

        if (!confirmPsd.contains(psd)){
            GBToast.shortToast("两次输入密码不一致");
            return;
        }

        if (GBUtil.stringIsEmpty(code)){
            GBToast.shortToast("验证码不能为空");
            return;
        }

        String email = "";
        if (!isPhoneLogin){
            email = phone;
            phone = "";
        }
        Bid.RetrieveLoginPwRequest request = Bid.RetrieveLoginPwRequest.newBuilder()
                .setMsgCode(code)
                .setPhone(phone)
                .setPhoneCode(phoneCode)
                .setNewPw(GBUtil.getSHA256(psd))
                .setEmail(email)
                .build();
        GBNetRequest.stub().retrieveLoginPw(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener() {
            @Override
            public void complete(Object value, GBError error) {
                if (error == null){
                    submitFinish();
                }
            }
        }));
    }

    void submitFinish(){
        Intent intent = new Intent(mContext, TransitionPage.class);
        intent.putExtra(TransitionPage.Key_title,"修改成功");
        intent.putExtra(TransitionPage.Key_image,R.drawable.result_submit_success);
        jumpActivityIntent(intent);
    }
}